package util;

import java.sql.SQLException;
import java.util.*;

import model.*;
import dal.*;

public class CacheUtil {

	public static void addToCache(IModel obj) throws Exception {
		if (obj instanceof AgendaItemModel) { // AgendaItem
			_agendaItemChanged = true;
			// ModelUtil.addToList(_agendaItemList, (AgendaItemModel) obj);
		} else if (obj instanceof AgendaModel) { // Agenda
			_agendaChanged = true;
			// ModelUtil.addToList(_agendaList, (AgendaModel) obj);
		} else if (obj instanceof ChecklistModel) { // Checklist
			_checklistChanged = true;
			// ModelUtil.addToList(_checklistList, (ChecklistModel) obj);
		} else if (obj instanceof ChecklistItemModel) { // ChecklistItem
			_checklistItemChanged = true;
			// ModelUtil.addToList(_checklistItemList, (ChecklistItemModel)
			// obj);
		} else if (obj instanceof QuotationModel) { // Quotaion
			_quotationChanged = true;
			// ModelUtil.addToList(_quotationList, (QuotationModel) obj);
		} else if (obj instanceof QuotationItemModel) { // QuotationItem
			_quotationItemChanged = true;
			// ModelUtil.addToList(_quotationItemList, (QuotationItemModel)
			// obj);
		} else if (obj instanceof EventModel) { // Event
			_eventChanged = true;
			// ModelUtil.addToList(_eventList, (EventModel) obj);
		} else if (obj instanceof ProjectModel) { // Project
			_projectChanged = true;
			// ModelUtil.addToList(_projectList, (ProjectModel) obj);
		} else if (obj instanceof CompanyModel) { // Company
			_companyChanged = true;
			// ModelUtil.addToList(_companyList, (CompanyModel) obj);
		} else if (obj instanceof EmployeeModel) { // Employee
			_employeeChanged = true;
			// ModelUtil.addToList(_employeeList, (EmployeeModel) obj);
		} else if (obj instanceof UnitModel) { // Unit
			_unitChanged = true;
			// ModelUtil.addToList(_unitList, (UnitModel) obj);
		} else if (obj instanceof VenueModel) { // Venue
			_venueChanged = true;
			// ModelUtil.addToList(_venueList, (VenueModel) obj);
		} else if (obj instanceof ProjectTypeModel) { // ProjectType
			_projectTypeChanged = true;
			// ModelUtil.addToList(_projectTypeList, (ProjectTypeModel) obj);
		} else if (obj instanceof PhaseModel) { // PhaseModel
			_phaseChanged = true;
		} else if (obj instanceof PlaceTypeModel) { // PlaceTypeModel
			_placeTypeChanged = true;
		} else if (obj instanceof PlaceModel) { // PlaceModel
			_placeChanged = true;
		} else if (obj instanceof VenueTypeModel) { // VenueTypeModel
			_venueTypeChanged = true;
		} else if (obj instanceof CurrencyModel) { // CurrencyModel
			_currencyChanged = true;
		} else if (obj instanceof ServiceModel) { // ServiceModel
			_serviceChanged = true;
		} else if (obj instanceof ProductModel) { // ProductModel
			_productChanged = true;
		} else if (obj instanceof DocumentTypeModel) { // DocumentTypeModel
			_documentTypeChanged = true;
		} else if (obj instanceof ProjectRoleModel) { // ProjectRoleModel
			_projectRoleChanged = true;
		} else {
			// TODO more class here
		}
	}

	public static void updateCache(IModel obj) throws SQLException,
			ClassNotFoundException {
		if (obj instanceof AgendaModel) {
			_agendaChanged = true;
			// ModelUtil.updateList(_agendaList, (AgendaModel) obj);
		} else if (obj instanceof AgendaItemModel) {
			_agendaItemChanged = true;
			// ModelUtil.updateList(_agendaItemList, (AgendaItemModel) obj);
		} else if (obj instanceof ChecklistModel) {
			_checklistChanged = true;
			// ModelUtil.updateList(_checklistList, (ChecklistModel) obj);
		} else if (obj instanceof ChecklistItemModel) {
			_checklistItemChanged = true;
			// ModelUtil.updateList(_checklistItemList, (ChecklistItemModel)
			// obj);
		} else if (obj instanceof QuotationModel) {
			_quotationChanged = true;
			// ModelUtil.updateList(_quotationList, (QuotationModel) obj);
		} else if (obj instanceof QuotationItemModel) {
			_quotationItemChanged = true;
			// ModelUtil.updateList(_quotationItemList, (QuotationItemModel)
			// obj);
		} else if (obj instanceof CompanyModel) {
			_companyChanged = true;
			// ModelUtil.updateList(_companyList, (CompanyModel) obj);
		} else if (obj instanceof EmployeeModel) {
			_employeeChanged = true;
			// ModelUtil.updateList(_employeeList, (EmployeeModel) obj);
		} else if (obj instanceof EventModel) {
			_eventChanged = true;
			// ModelUtil.updateList(_eventList, (EventModel) obj);
		} else if (obj instanceof ProjectModel) {
			_projectChanged = true;
			// ModelUtil.updateList(_projectList, (ProjectModel) obj);
		} else if (obj instanceof ProjectTypeModel) {
			_projectTypeChanged = true;
			// ModelUtil.updateList(_projectTypeList, (ProjectTypeModel) obj);
		} else if (obj instanceof UnitModel) {
			_unitChanged = true;
			// ModelUtil.updateList(_unitList, (UnitModel) obj);
		} else if (obj instanceof VenueModel) {
			_venueChanged = true;
			// ModelUtil.removeFromList(_venueList, (VenueModel) obj);
			// ModelUtil.updateList(_venueList, (VenueModel) obj);
		} else if (obj instanceof PlaceTypeModel) {
			_placeTypeChanged = true;
			// ModelUtil.updateList(_placeTypeList, (PlaceTypeModel) obj);
		} else if (obj instanceof PlaceModel) { // PlaceModel
			_placeChanged = true;
		} else if (obj instanceof VenueTypeModel) { // VenueTypeModel
			_venueTypeChanged = true;
		} else if (obj instanceof CurrencyModel) { // CurrencyModel
			_currencyChanged = true;
		} else if (obj instanceof ServiceModel) { // ServiceModel
			_serviceChanged = true;
		} else if (obj instanceof ProductModel) { // ProductModel
			_productChanged = true;
		} else if (obj instanceof DocumentTypeModel) { // DocumentTypeModel
			_documentTypeChanged = true;
		} else if (obj instanceof ProjectRoleModel) { // ProjectRoleModel
			_projectRoleChanged = true;
		} else {
			// TODO more class here
		}
	}

	public static void deleteCache(IModel obj) throws SQLException,
			ClassNotFoundException {
		if (obj instanceof AgendaModel) {
			ModelUtil.removeFromList(_agendaList, (AgendaModel) obj);
		} else if (obj instanceof AgendaItemModel) {
			ModelUtil.removeFromList(_agendaItemList, (AgendaItemModel) obj);
		} else if (obj instanceof ChecklistModel) {
			ModelUtil.removeFromList(_checklistList, (ChecklistModel) obj);
		} else if (obj instanceof ChecklistItemModel) {
			ModelUtil.removeFromList(_checklistItemList,
					(ChecklistItemModel) obj);
		} else if (obj instanceof QuotationModel) {
			ModelUtil.removeFromList(_quotationList, (QuotationModel) obj);
		} else if (obj instanceof QuotationItemModel) {
			ModelUtil.removeFromList(_quotationItemList,
					(QuotationItemModel) obj);// ///////////////////////////
		} else if (obj instanceof CompanyModel) {
			ModelUtil.removeFromList(_companyList, (CompanyModel) obj);
		} else if (obj instanceof EmployeeModel) {
			ModelUtil.removeFromList(_employeeList, (EmployeeModel) obj);
		} else if (obj instanceof EventModel) {
			ModelUtil.removeFromList(_eventList, (EventModel) obj);
		} else if (obj instanceof ProjectModel) {
			ModelUtil.removeFromList(_projectList, (ProjectModel) obj);
		} else if (obj instanceof ProjectTypeModel) {
			ModelUtil.removeFromList(_projectTypeList, (ProjectTypeModel) obj);
		} else if (obj instanceof UnitModel) {
			ModelUtil.removeFromList(_unitList, (UnitModel) obj);
		} else if (obj instanceof VenueModel) {
			ModelUtil.removeFromList(_venueList, (VenueModel) obj);
		} else if (obj instanceof PlaceTypeModel) {
			ModelUtil.removeFromList(_placeTypeList, (PlaceTypeModel) obj);
		} else if (obj instanceof PlaceModel) {
			ModelUtil.removeFromList(_placeList, (PlaceModel) obj);
		} else if (obj instanceof CurrencyModel) {
			ModelUtil.removeFromList(_currencyList, (CurrencyModel) obj);
		} else if (obj instanceof VenueTypeModel) {
			ModelUtil.removeFromList(_venueTypeList, (VenueTypeModel) obj);
		} else if (obj instanceof ServiceModel) {
			ModelUtil.removeFromList(_serviceList, (ServiceModel) obj);
		} else if (obj instanceof ProductModel) {
			ModelUtil.removeFromList(_productList, (ProductModel) obj);
		} else if (obj instanceof DocumentTypeModel) {
			ModelUtil
					.removeFromList(_documentTypeList, (DocumentTypeModel) obj);
		} else {
			// TODO more class here
		}
	}

	// PROJECT TYPE

	private static List<ProjectTypeModel> _projectTypeList = null;
	private static boolean _projectTypeChanged = false;

	private static void loadProjectTypeList() throws Exception {
		if (_projectTypeList == null || _projectTypeChanged) {
			GenericDal<ProjectTypeModel, ProjectTypeModelExt> dalProjectType = new GenericDal<ProjectTypeModel, ProjectTypeModelExt>(
					new ProjectTypeModelExt());
			ProjectTypeModelExt queryProjectType = new ProjectTypeModelExt();
			_projectTypeList = dalProjectType.selectList(queryProjectType);
			_projectTypeChanged = false;
		}
	}

	public static List<ProjectTypeModel> getProjectTypeList() throws Exception {
		loadProjectTypeList();

		Comparator<ProjectTypeModel> comparator = new GenericComparator<ProjectTypeModel>(
				ProjectTypeModel.FieldNames.DisplayIndex, false);
		Collections.sort(_projectTypeList, comparator);

		return _projectTypeList;
	}

	public static List<ProjectTypeModel> getProjectTypeList(boolean sorted,
			String sortField) throws Exception {
		loadProjectTypeList();

		Comparator<ProjectTypeModel> comparator = new GenericComparator<ProjectTypeModel>(
				ProjectTypeModel.FieldNames.DisplayIndex, false);
		Collections.sort(_projectTypeList, comparator);

		return _projectTypeList;
	}

	// END OF PROJECT TYPE

	// PROJECT TEMPLATE

	private static List<ProjectModel> _projectTemplateList = null;
	private static boolean _projectTemplateChanged = false;

	private static void loadProjectTemplateList() throws Exception {
		if (_projectTemplateList == null || _projectTemplateChanged) {
			GenericDal<ProjectModel, ProjectModelExt> dalProjectTemplate = new GenericDal<ProjectModel, ProjectModelExt>(
					new ProjectModelExt());

			ProjectModelExt query = new ProjectModelExt();

			// TODO set isTemplate here
			// query.setIsTemplate(true);

			_projectTemplateList = dalProjectTemplate.selectList(query);
			_projectTemplateChanged = false;
		}
	}

	public static List<ProjectModel> getProjectTemplateList() throws Exception {
		loadProjectTemplateList();

		// Comparator<ProjectTypeModel> comparator = new
		// GenericComparator<ProjectTypeModel>(
		// ProjectTypeModel.FieldNames.DisplayIndex, false);
		// Collections.sort(_projectTemplateList, comparator);

		return _projectTemplateList;
	}

	public static List<ProjectModel> getProjectTemplateList(boolean sorted,
			String sortField) throws Exception {
		loadProjectTemplateList();

		// Comparator<ProjectTypeModel> comparator = new
		// GenericComparator<ProjectTypeModel>(
		// ProjectTypeModel.FieldNames.DisplayIndex, false);
		// Collections.sort(_projectTemplateList, comparator);

		return _projectTemplateList;
	}

	// END OF PROJECT TEMPLATE

	// PLACE TYPE

	private static List<PlaceTypeModel> _placeTypeList = null;
	private static boolean _placeTypeChanged = false;

	private static void loadPlaceTypeList() throws Exception {
		if (_placeTypeList == null || _placeTypeChanged) {
			GenericDal<PlaceTypeModel, PlaceTypeModelExt> dalPlaceType = new GenericDal<PlaceTypeModel, PlaceTypeModelExt>(
					new PlaceTypeModelExt());
			PlaceTypeModelExt queryPlaceType = new PlaceTypeModelExt();
			_placeTypeList = dalPlaceType.selectList(queryPlaceType);
			_placeTypeChanged = false;
		}
	}

	public static List<PlaceTypeModel> getPlaceTypeList() throws Exception {
		loadPlaceTypeList();
		// TODO : sort
		return _placeTypeList;
	}

	public static List<PlaceTypeModel> getPlaceTypeList(boolean sorted,
			String sortField) throws Exception {
		loadPlaceTypeList();
		// TODO : sort
		return _placeTypeList;
	}

	public static String getPlaceTypeName(int idPlaceType) throws Exception {
		String name = "";
		loadPlaceTypeList();
		PlaceTypeModel obj = ModelUtil.<PlaceTypeModel> getFromList(
				_placeTypeList, idPlaceType);
		if (obj != null && obj.getName() != null) {
			name = obj.getName();
		}
		return name;
	}

	// END OF PLACE TYPE
	// PLACE

	private static List<PlaceModel> _placeList = null;
	private static boolean _placeChanged = false;

	private static void loadPlaceList() throws Exception {
		if (_placeList == null || _placeChanged) {
			GenericDal<PlaceModel, PlaceModelExt> dalPlace = new GenericDal<PlaceModel, PlaceModelExt>(
					new PlaceModelExt());
			PlaceModelExt queryPlace = new PlaceModelExt();
			_placeList = dalPlace.selectList(queryPlace);
			_placeChanged = false;
		}
	}

	public static List<PlaceModel> getPlaceList() throws Exception {
		loadPlaceList();
		// TODO : sort
		return _placeList;
	}

	public static List<PlaceModel> getPlaceList(boolean sorted, String sortField)
			throws Exception {
		loadPlaceList();
		// TODO : sort
		return _placeList;
	}

	public static String getPlaceName(int idPlace) throws Exception {
		String name = "";
		loadPlaceList();
		PlaceModel obj = ModelUtil
				.<PlaceModel> getFromList(_placeList, idPlace);
		if (obj != null && obj.getName() != null) {
			name = obj.getName();
		}
		return name;
	}

	// Dat add getLastPlaceId method
	public static int getLastPlaceId() throws Exception {
		int lastId = Integer.MIN_VALUE;
		loadPlaceList();
		lastId = ModelUtil.<PlaceModel> getLastId(_placeList);
		return lastId;

	}

	//

	// END OF PLACE

	// COMPANY

	private static List<CompanyModel> _companyList = null;
	private static boolean _companyChanged = false;

	private static void loadCompanyList() throws Exception {
		if (_companyList == null || _companyChanged) {
			GenericDal<CompanyModel, CompanyModelExt> dalCompany = new GenericDal<CompanyModel, CompanyModelExt>(
					new CompanyModelExt());
			CompanyModelExt queryCompany = new CompanyModelExt();
			_companyList = dalCompany.selectList(queryCompany);
			_companyChanged = false;
		}
	}

	public static List<CompanyModel> getCompanyList() throws Exception {
		loadCompanyList();
		// TODO : sort
		return _companyList;
	}

	public static List<CompanyModel> getCompanyList(boolean sorted,
			String sortField) throws Exception {
		loadCompanyList();
		// TODO : sort
		return _companyList;
	}

	public static String getCompanyName(int idCompany) throws Exception {
		String name = "";
		loadCompanyList();
		CompanyModel obj = ModelUtil.<CompanyModel> getFromList(_companyList,
				idCompany);
		if (obj != null && obj.getName() != null) {
			name = obj.getName();
		}
		return name;
	}

	// END OF COMPANY

	// PHASE begin

	private static List<PhaseModel> _phaseList = null;
	private static boolean _phaseChanged = false;

	public static List<PhaseModel> getPhaseList(int idProject) throws Exception {
		PhaseModelExt query = new PhaseModelExt();
		query.setIdProject(idProject);

		GenericDal<PhaseModel, PhaseModelExt> dal = new GenericDal<PhaseModel, PhaseModelExt>(
				new PhaseModelExt());
		List<PhaseModel> list = dal.selectList(query);
		return list;
	}

	private static void loadPhaseList() throws Exception {
		if (_phaseList == null || _phaseChanged) {

			GenericDal<PhaseModel, PhaseModelExt> dalPhase = new GenericDal<PhaseModel, PhaseModelExt>(
					new PhaseModelExt());
			PhaseModelExt queryPhase = new PhaseModelExt();

			if (_phaseList == null) {
				// first time
				_phaseList = dalPhase.selectList(queryPhase);
			} else if (_phaseChanged) {
				Date lastUpdated = ModelUtil.getLastUpdated(_phaseList);

				// load records that have changed after the last updated
				queryPhase.setUpdatedFrom(lastUpdated);

				List<PhaseModel> list = dalPhase.selectList(queryPhase);

				// update new or changed records to cache
				ModelUtil.updateList(_phaseList, list);
			}

			// set vanue changed as false
			_phaseChanged = false;
		}
	}

	public static List<PhaseModel> getPhaseList() throws Exception {
		loadPhaseList();
		return _phaseList;
	}

	public static List<PhaseModel> getPhaseList(boolean sorted,
			Comparator<PhaseModel> comparator) throws Exception {
		loadPhaseList();
		Collections.sort(_phaseList, comparator);
		return _phaseList;
	}

	public static String getPhaseName(int idPhase) throws Exception {
		String name = "";
		loadPhaseList();
		PhaseModel obj = ModelUtil
				.<PhaseModel> getFromList(_phaseList, idPhase);
		if (obj != null && obj.getName() != null) {
			name = obj.getName();
		}
		return name;
	}

	// PHASE end

	// VENUE begin

	private static List<VenueModel> _venueList = null;
	private static boolean _venueChanged = false;

	public static List<VenueModel> getVenueList(int idPlace) throws Exception {
		VenueModelExt query = new VenueModelExt();
		query.setIdPlace(idPlace);

		GenericDal<VenueModel, VenueModelExt> dal = new GenericDal<VenueModel, VenueModelExt>(
				new VenueModelExt());
		List<VenueModel> list = dal.selectList(query);
		return list;
	}

	private static void loadVenueList() throws Exception {
		if (_venueList == null || _venueChanged) {

			GenericDal<VenueModel, VenueModelExt> dalVenue = new GenericDal<VenueModel, VenueModelExt>(
					new VenueModelExt());
			VenueModelExt queryVenue = new VenueModelExt();

			if (_venueList == null) {
				// first time
				_venueList = dalVenue.selectList(queryVenue);
			} else if (_venueChanged) {
				Date lastUpdated = ModelUtil.getLastUpdated(_venueList);

				// load records that have changed after the last updated
				queryVenue.setUpdatedFrom(lastUpdated);

				List<VenueModel> list = dalVenue.selectList(queryVenue);

				// update new or changed records to cache
				ModelUtil.updateList(_venueList, list);
			}

			// set vanue changed as false
			_venueChanged = false;
		}
	}

	public static List<VenueModel> getVenueList() throws Exception {
		loadVenueList();
		return _venueList;
	}

	public static List<VenueModel> getVenueList(boolean sorted,
			Comparator<VenueModel> comparator) throws Exception {
		loadVenueList();
		Collections.sort(_venueList, comparator);
		return _venueList;
	}

	public static String getVenueName(int idVenue) throws Exception {
		String name = "";
		loadVenueList();
		VenueModel obj = ModelUtil
				.<VenueModel> getFromList(_venueList, idVenue);
		if (obj != null && obj.getName() != null) {
			name = obj.getName();
		}
		return name;
	}

	// VENUE end

	// VENUE TYPE

	private static List<VenueTypeModel> _venueTypeList = null;
	private static boolean _venueTypeChanged = false;

	private static void loadVenueTypeList() throws Exception {
		if (_venueTypeList == null || _venueTypeChanged) {
			GenericDal<VenueTypeModel, VenueTypeModelExt> dalVenueType = new GenericDal<VenueTypeModel, VenueTypeModelExt>(
					new VenueTypeModelExt());
			VenueTypeModelExt queryVenueType = new VenueTypeModelExt();
			_venueTypeList = dalVenueType.selectList(queryVenueType);
			_venueTypeChanged = false;
		}
	}

	public static List<VenueTypeModel> getVenueTypeList() throws Exception {
		loadVenueTypeList();
		// TODO : sort
		return _venueTypeList;
	}

	public static List<VenueTypeModel> getVenueTypeList(boolean sorted,
			String sortField) throws Exception {
		loadVenueTypeList();
		// TODO : sort
		return _venueTypeList;
	}

	public static String getVenueTypeName(int idVenueType) throws Exception {
		String name = "";
		loadVenueTypeList();
		VenueTypeModel obj = ModelUtil.<VenueTypeModel> getFromList(
				_venueTypeList, idVenueType);
		if (obj != null && obj.getName() != null) {
			name = obj.getName();
		}
		return name;
	}

	// END OF VENUE TYPE

	// Quotation
	private static List<QuotationModel> _quotationList = null;
	private static boolean _quotationChanged = false;

	private static void loadQuotationList() throws Exception {
		if (_quotationList == null || _quotationChanged) {
			GenericDal<QuotationModel, QuotationModelExt> dalQuotation = new GenericDal<QuotationModel, QuotationModelExt>(
					new QuotationModelExt());
			QuotationModelExt queryQuotation = new QuotationModelExt();
			_quotationList = dalQuotation.selectList(queryQuotation);
			_quotationChanged = false;
		}
	}

	public static List<QuotationModel> getQuotationList() throws Exception {
		loadQuotationList();
		// TODO : sort
		return _quotationList;
	}

	public static List<QuotationModel> getQuotationList(int idProject)
			throws Exception {
		QuotationModelExt query = new QuotationModelExt();
		query.setIdProject(idProject);

		GenericDal<QuotationModel, QuotationModelExt> dal = new GenericDal<QuotationModel, QuotationModelExt>(
				new QuotationModelExt());
		List<QuotationModel> list = dal.selectList(query);
		return list;
	}

	public static List<QuotationModel> getQuotationList(boolean sorted,
			String sortField) throws Exception {
		loadQuotationList();
		// TODO : sort
		return _quotationList;
	}

	public static String getQuotationName(int idQuotation) throws Exception {
		String name = "";
		loadQuotationList();
		QuotationModel obj = ModelUtil.<QuotationModel> getFromList(
				_quotationList, idQuotation);
		if (obj != null && obj.getName() != null) {
			name = obj.getName();
		}
		return name;
	}

	public static QuotationModel getQuotation(int idQuotation) throws Exception {
		loadQuotationList();
		QuotationModel obj = ModelUtil.<QuotationModel> getFromList(
				_quotationList, idQuotation);
		return obj;
	}

	// End of Quotation

	// QuotationItem
	private static List<QuotationItemModel> _quotationItemList = null;
	private static boolean _quotationItemChanged = false;

	private static void loadQuotationItemList() throws Exception {
		if (_quotationItemList == null || _quotationItemChanged) {
			GenericDal<QuotationItemModel, QuotationItemModelExt> dalQuotationItem = new GenericDal<QuotationItemModel, QuotationItemModelExt>(
					new QuotationItemModelExt());
			QuotationItemModelExt queryQuotationItem = new QuotationItemModelExt();
			_quotationItemList = dalQuotationItem
					.selectList(queryQuotationItem);
			_quotationItemChanged = false;
		}
	}

	public static List<QuotationItemModel> getQuotationItemList()
			throws Exception {
		loadQuotationItemList();
		// TODO : sort
		return _quotationItemList;
	}

	public static List<QuotationItemModel> getQuotationItemList(int idQuotation)
			throws Exception {
		QuotationItemModelExt query = new QuotationItemModelExt();
		query.setIdQuotation(idQuotation);

		GenericDal<QuotationItemModel, QuotationItemModelExt> dal = new GenericDal<QuotationItemModel, QuotationItemModelExt>(
				new QuotationItemModelExt());
		List<QuotationItemModel> list = dal.selectList(query);
		return list;
	}

	public static List<QuotationItemModel> getQuotationItemList(boolean sorted,
			String sortField) throws Exception {
		loadQuotationItemList();
		// TODO : sort
		return _quotationItemList;
	}

	public static List<QuotationItemModel> getIsGroupQuotationItemList(
			int idQuotation) throws Exception {
		QuotationItemModelExt query = new QuotationItemModelExt();
		query.setIdQuotation(idQuotation);
		query.setIsGroup(true);
		GenericDal<QuotationItemModel, QuotationItemModelExt> dal = new GenericDal<QuotationItemModel, QuotationItemModelExt>(
				new QuotationItemModelExt());
		List<QuotationItemModel> list = dal.selectList(query);
		return list;
	}

	// public static List<QuotationItemModel> getQuotationItemList() throws
	// Exception {
	//
	// // loadQuotationItemList();
	//
	// // TODO : sort
	//
	// return _quotationItemList;
	// }
	//
	// public static List<QuotationItemModel> getQuotationItemList(boolean
	// sorted, String sortField) throws Exception {
	//
	// // loadQuotationItemList();
	//
	// // TODO : sort
	//
	// return _quotationItemList;
	// }

	public static String getQuotationItemName(int idQuotationItem)
			throws Exception {
		loadQuotationItemList();
		String name = "";
		QuotationItemModel obj = ModelUtil.<QuotationItemModel> getFromList(
				_quotationItemList, idQuotationItem);
		if (obj != null && obj.getName() != null) {
			name = obj.getName();
		}
		return name;
	}

	// End of QuotationItem

	// Product
	private static List<ProductModel> _productList = null;
	private static boolean _productChanged = false;

	private static void loadProductList() throws Exception {
		if (_productList == null || _productChanged) {
			GenericDal<ProductModel, ProductModelExt> dalProduct = new GenericDal<ProductModel, ProductModelExt>(
					new ProductModelExt());
			ProductModelExt queryProduct = new ProductModelExt();
			_productList = dalProduct.selectList(queryProduct);
			_productChanged = false;
		}
	}

	public static List<ProductModel> getProductList() throws Exception {
		loadProductList();
		// TODO : sort
		return _productList;
	}

	public static List<ProductModel> getProductList(boolean sorted,
			String sortField) throws Exception {
		loadProductList();
		// TODO : sort
		return _productList;
	}

	public static String getProductName(int idProduct) throws Exception {
		String name = "";
		loadProductList();
		ProductModel obj = ModelUtil.<ProductModel> getFromList(_productList,
				idProduct);
		if (obj != null && obj.getName() != null) {
			name = obj.getName();
		}
		return name;
	}

	// End of Product

	// UNIT

	private static List<UnitModel> _unitList = null;
	private static boolean _unitChanged = false;

	private static void loadUnitList() throws Exception {
		if (_unitList == null || _unitChanged) {
			GenericDal<UnitModel, UnitModelExt> dalUnit = new GenericDal<UnitModel, UnitModelExt>(
					new UnitModelExt());
			UnitModelExt queryUnit = new UnitModelExt();
			_unitList = dalUnit.selectList(queryUnit);
			_unitChanged = false;
		}
	}

	public static List<UnitModel> getUnitList() throws Exception {
		loadUnitList();
		// TODO : sort
		return _unitList;
	}

	public static List<UnitModel> getUnitList(boolean sorted, String sortField)
			throws Exception {

		loadUnitList();

		// TODO : sort

		return _unitList;
	}

	public static String getUnitName(int idUnit) throws Exception {
		String name = "";
		loadUnitList();
		UnitModel obj = ModelUtil.<UnitModel> getFromList(_unitList, idUnit);
		if (obj != null && obj.getName() != null) {
			name = obj.getName();
		}
		return name;
	}

	// END OF UNIT

	// SERVICE

	private static List<ServiceModel> _serviceList = null;
	private static boolean _serviceChanged = false;

	private static void loadServiceList() throws Exception {
		if (_serviceList == null || _serviceChanged) {
			GenericDal<ServiceModel, ServiceModelExt> dalService = new GenericDal<ServiceModel, ServiceModelExt>(
					new ServiceModelExt());
			ServiceModelExt queryService = new ServiceModelExt();
			_serviceList = dalService.selectList(queryService);
			_serviceChanged = false;
		}
	}

	public static List<ServiceModel> getServiceList() throws Exception {

		loadServiceList();

		// TODO : sort

		return _serviceList;
	}

	public static List<ServiceModel> getServiceList(boolean sorted,
			String sortField) throws Exception {

		loadServiceList();

		// TODO : sort

		return _serviceList;
	}

	public static String getServiceName(int idService) throws Exception {
		String name = "";
		loadServiceList();
		ServiceModel obj = ModelUtil.<ServiceModel> getFromList(_serviceList,
				idService);
		if (obj != null && obj.getName() != null) {
			name = obj.getName();
		}
		return name;
	}

	// END OF SERVICE

	// CURRENCY
	private static List<CurrencyModel> _currencyList = null;
	private static boolean _currencyChanged = false;

	private static void loadCurrencyList() throws Exception {
		if (_currencyList == null || _currencyChanged) {
			GenericDal<CurrencyModel, CurrencyModelExt> dalCurrency = new GenericDal<CurrencyModel, CurrencyModelExt>(
					new CurrencyModelExt());
			CurrencyModelExt queryCurrency = new CurrencyModelExt();
			_currencyList = dalCurrency.selectList(queryCurrency);
			_currencyChanged = false;
		}
	}

	public static List<CurrencyModel> getCurrencyList() throws Exception {

		loadCurrencyList();

		// TODO : sort

		return _currencyList;
	}

	public static List<CurrencyModel> getCurrencyList(boolean sorted,
			String sortField) throws Exception {

		loadCurrencyList();

		// TODO : sort

		return _currencyList;
	}

	public static String getCurrencyName(int idCurrency) throws Exception {
		String name = "";
		loadCurrencyList();
		CurrencyModel obj = ModelUtil.<CurrencyModel> getFromList(
				_currencyList, idCurrency);
		if (obj != null && obj.getName() != null) {
			name = obj.getName();
		}
		return name;
	}

	public static int getDefaultCurrencyId() throws Exception {
		int DefaultCurrencyId = Integer.MIN_VALUE;
		loadCurrencyList();
		for (CurrencyModel t : _currencyList) {
			if (t.getIsDefault()) {
				DefaultCurrencyId = t.getId();
			}
		}
		return DefaultCurrencyId;

	}

	// END OF CURRENCY

	// PROJECT
	private static List<ProjectModel> _projectList = null;
	private static boolean _projectChanged = false;

	private static void loadProjectList() throws Exception {
		if (_projectList == null || _projectChanged) {
			GenericDal<ProjectModel, ProjectModelExt> dalProject = new GenericDal<ProjectModel, ProjectModelExt>(
					new ProjectModelExt());
			ProjectModelExt queryProject = new ProjectModelExt();
			queryProject.setOffset(Integer.MIN_VALUE);
			queryProject.setLimit(Integer.MIN_VALUE);

			_projectList = dalProject.selectList(queryProject);

			_projectChanged = false;
		}
	}

	public static List<ProjectModel> getProjectList() throws Exception {

		loadProjectList();

		// TODO : sort

		return _projectList;
	}

	public static List<ProjectModel> getProjectList(boolean sorted,
			String sortField) throws Exception {

		loadProjectList();

		// TODO : sort

		return _projectList;
	}

	public static String getProjectName(int idProject) throws Exception {
		String name = "";
		loadProjectList();
		ProjectModel obj = ModelUtil.<ProjectModel> getFromList(_projectList,
				idProject);
		if (obj != null && obj.getName() != null) {
			name = obj.getName();
		}
		return name;
	}

	// Dat add getLastPlaceId method
	public static int getLastProjectId() throws Exception {
		int lastId = Integer.MIN_VALUE;
		loadProjectList();
		lastId = ModelUtil.<ProjectModel> getLastId(_projectList);
		return lastId;

	}

	//

	// END OF PROJECT

	// EMPLOYEE
	private static List<EmployeeModel> _employeeList = null;
	private static boolean _employeeChanged = false;

	private static void loadEmployeeList() throws Exception {
		if (_employeeList == null || _employeeChanged) {
			GenericDal<EmployeeModel, EmployeeModelExt> dalEmployee = new GenericDal<EmployeeModel, EmployeeModelExt>(
					new EmployeeModelExt());
			EmployeeModelExt queryEmployee = new EmployeeModelExt();
			queryEmployee.setOffset(Integer.MIN_VALUE);
			queryEmployee.setLimit(Integer.MIN_VALUE);
			_employeeList = dalEmployee.selectList(queryEmployee);
			_employeeChanged = false;
		}
	}

	public static List<EmployeeModel> getEmployeeList() throws Exception {

		loadEmployeeList();

		// TODO : sort

		return _employeeList;
	}

	public static List<EmployeeModel> getEmployeeList(boolean sorted,
			String sortField) throws Exception {

		loadEmployeeList();

		// TODO : sort

		return _employeeList;
	}

	public static List<EmployeeModel> getPMList() throws Exception {

		loadEmployeeList();

		// TODO : sort

		return _employeeList;
	}

	public static String getEmployeeName(int idEmployee) throws Exception {
		String name = "";
		loadEmployeeList();
		EmployeeModel obj = ModelUtil.<EmployeeModel> getFromList(
				_employeeList, idEmployee);
		if (obj != null && obj.getName() != null) {
			name = obj.getName();
		}
		return name;
	}

	// END OF EMPLOYEE

	// PROJECT ROLE
	private static List<ProjectRoleModel> _projectRoleList = null;
	private static boolean _projectRoleChanged = false;

	private static void loadProjectRoleList() throws Exception {
		if (_projectRoleList == null || _projectRoleChanged) {
			GenericDal<ProjectRoleModel, ProjectRoleModelExt> dalProjectRole = new GenericDal<ProjectRoleModel, ProjectRoleModelExt>(
					new ProjectRoleModelExt());
			ProjectRoleModelExt queryProjectRole = new ProjectRoleModelExt();
			queryProjectRole.setOffset(Integer.MIN_VALUE);
			queryProjectRole.setLimit(Integer.MIN_VALUE);
			_projectRoleList = dalProjectRole.selectList(queryProjectRole);
			_projectRoleChanged = false;
		}
	}

	public static List<ProjectRoleModel> getProjectRoleList() throws Exception {

		loadProjectRoleList();

		// TODO : sort

		return _projectRoleList;
	}

	public static List<ProjectRoleModel> getProjectRoleList(boolean sorted,
			String sortField) throws Exception {

		loadProjectRoleList();

		// TODO : sort

		return _projectRoleList;
	}

	public static String getProjectRoleName(int idProjectRole) throws Exception {
		String name = "";
		loadProjectRoleList();
		ProjectRoleModel obj = ModelUtil.<ProjectRoleModel> getFromList(
				_projectRoleList, idProjectRole);
		if (obj != null && obj.getName() != null) {
			name = obj.getName();
		}
		return name;
	}

	// END OF PROJECT ROLE

	// Agenda
	private static List<AgendaModel> _agendaList = null;
	private static boolean _agendaChanged = false;

	private static void loadAgendaList() throws Exception {

		if (_agendaList == null || _agendaChanged) {
			GenericDal<AgendaModel, AgendaModelExt> dalAgenda = new GenericDal<AgendaModel, AgendaModelExt>(
					new AgendaModelExt());
			AgendaModelExt queryAgenda = new AgendaModelExt();
			queryAgenda.setOffset(Integer.MIN_VALUE);
			queryAgenda.setLimit(Integer.MIN_VALUE);

			_agendaList = dalAgenda.selectList(queryAgenda);

			_agendaChanged = false;
		}
	}

	public static List<AgendaModel> getAgendaList() throws Exception {

		loadAgendaList();

		// TODO : sort

		return _agendaList;
	}

	public static List<AgendaModel> getAgendaList(boolean sorted,
			String sortField) throws Exception {

		loadAgendaList();

		// TODO : sort

		return _agendaList;
	}

	public static String getAgendaName(int idAgenda) throws Exception {
		String name = "";
		loadAgendaList();
		AgendaModel obj = ModelUtil.<AgendaModel> getFromList(_agendaList,
				idAgenda);
		if (obj != null && obj.getName() != null) {
			name = obj.getName();
		}
		return name;
	}

	public static AgendaModel getAgenda(int idAgenda) throws Exception {
		loadAgendaList();
		AgendaModel obj = ModelUtil.<AgendaModel> getFromList(_agendaList,
				idAgenda);
		return obj;
	}

	// End of Agenda

	// AGENDAITEM
	private static List<AgendaItemModel> _agendaItemList = null;
	private static boolean _agendaItemChanged = false;

	private static void loadAgendaItemList() throws Exception {

		if (_agendaItemList == null || _agendaItemChanged) {
			GenericDal<AgendaItemModel, AgendaItemModelExt> dalAgendaItem = new GenericDal<AgendaItemModel, AgendaItemModelExt>(
					new AgendaItemModelExt());
			AgendaItemModelExt queryAgendaItem = new AgendaItemModelExt();
			queryAgendaItem.setOffset(Integer.MIN_VALUE);
			queryAgendaItem.setLimit(Integer.MIN_VALUE);

			_agendaItemList = dalAgendaItem.selectList(queryAgendaItem);

			_agendaItemChanged = false;
		}
	}

	public static List<AgendaItemModel> getAgendaItemList() throws Exception {

		loadAgendaItemList();

		// TODO : sort

		return _agendaItemList;
	}

	public static List<AgendaItemModel> getAgendaItemList(boolean sorted,
			String sortField) throws Exception {

		loadAgendaItemList();

		// TODO : sort

		return _agendaItemList;
	}

	public static List<AgendaItemModel> getIsGroupAgendaItemList(int iAgendaId)
			throws Exception {
		AgendaItemModelExt query = new AgendaItemModelExt();
		query.setIdAgenda(iAgendaId);
		query.setIsGroup(true);

		GenericDal<AgendaItemModel, AgendaItemModelExt> dal = new GenericDal<AgendaItemModel, AgendaItemModelExt>(
				new AgendaItemModelExt());
		List<AgendaItemModel> list = dal.selectList(query);
		return list;
	}

	public static List<AgendaItemModel> getSameParentAgendaItemList(
			int iParentId) throws Exception {
		AgendaItemModelExt query = new AgendaItemModelExt();
		query.setIdParent(iParentId);

		GenericDal<AgendaItemModel, AgendaItemModelExt> dal = new GenericDal<AgendaItemModel, AgendaItemModelExt>(
				new AgendaItemModelExt());
		List<AgendaItemModel> list = dal.selectList(query);
		return list;
	}

	public static String getAgendaItemName(int idAgendaItem) throws Exception {
		String name = "";
		loadAgendaItemList();
		AgendaItemModel obj = ModelUtil.<AgendaItemModel> getFromList(
				_agendaItemList, idAgendaItem);
		if (obj != null && obj.getName() != null) {
			name = obj.getName();
		}
		return name;
	}

	// END OF AGENDA ITEM

	// CHECKLIST
	private static List<ChecklistModel> _checklistList = null;
	private static boolean _checklistChanged = false;

	private static void loadChecklistList() throws Exception {

		if (_checklistList == null || _checklistChanged) {
			GenericDal<ChecklistModel, ChecklistModelExt> dalChecklist = new GenericDal<ChecklistModel, ChecklistModelExt>(
					new ChecklistModelExt());
			ChecklistModelExt queryChecklist = new ChecklistModelExt();
			queryChecklist.setOffset(Integer.MIN_VALUE);
			queryChecklist.setLimit(Integer.MIN_VALUE);

			_checklistList = dalChecklist.selectList(queryChecklist);

			_checklistChanged = false;
		}
	}

	public static List<ChecklistModel> getChecklistList() throws Exception {

		loadChecklistList();

		// TODO : sort

		return _checklistList;
	}

	public static List<ChecklistModel> getChecklistList(int idProject)
			throws Exception {
		ChecklistModelExt query = new ChecklistModelExt();
		query.setIdProject(idProject);

		GenericDal<ChecklistModel, ChecklistModelExt> dal = new GenericDal<ChecklistModel, ChecklistModelExt>(
				new ChecklistModelExt());
		List<ChecklistModel> list = dal.selectList(query);
		return list;
	}

	public static List<ChecklistModel> getChecklistList(boolean sorted,
			String sortField) throws Exception {

		loadChecklistList();

		// TODO : sort

		return _checklistList;
	}

	public static String getChecklistName(int idChecklist) throws Exception {
		String name = "";

		loadChecklistList();
		ChecklistModel obj = ModelUtil.<ChecklistModel> getFromList(
				_checklistList, idChecklist);
		if (obj != null && obj.getName() != null) {
			name = obj.getName();
		}

		return name;
	}

	public static ChecklistModel getChecklist(int idChecklist) throws Exception {
		loadChecklistList();
		ChecklistModel obj = ModelUtil.<ChecklistModel> getFromList(
				_checklistList, idChecklist);
		return obj;
	}

	// END OF CHECKLIST

	// ChecklistItem
	private static List<ChecklistItemModel> _checklistItemList = null;// Dat
																		// added
	private static boolean _checklistItemChanged = false;// Dat added

	public static List<ChecklistItemModel> getIsGroupChecklistItemList(
			int iChecklistId) throws Exception {
		ChecklistItemModelExt query = new ChecklistItemModelExt();
		query.setIdChecklist(iChecklistId);
		query.setIsGroup(true);

		GenericDal<ChecklistItemModel, ChecklistItemModelExt> dal = new GenericDal<ChecklistItemModel, ChecklistItemModelExt>(
				new ChecklistItemModelExt());
		List<ChecklistItemModel> list = dal.selectList(query);
		return list;
	}

	// Dat added loadChecklistItemList and getChecklistItemName method

	private static void loadChecklistItemList() throws Exception {

		if (_checklistItemList == null || _checklistItemChanged) {
			GenericDal<ChecklistItemModel, ChecklistItemModelExt> dalChecklistItem = new GenericDal<ChecklistItemModel, ChecklistItemModelExt>(
					new ChecklistItemModelExt());
			ChecklistItemModelExt queryChecklistItem = new ChecklistItemModelExt();
			queryChecklistItem.setOffset(Integer.MIN_VALUE);
			queryChecklistItem.setLimit(Integer.MIN_VALUE);

			_checklistItemList = dalChecklistItem
					.selectList(queryChecklistItem);

			_checklistItemChanged = false;
		}
	}

	public static List<ChecklistItemModel> getChecklistItemList()
			throws Exception {
		loadChecklistItemList();
		// TODO : sort
		return _checklistItemList;
	}

	public static List<ChecklistItemModel> getChecklistItemList(int idChecklist)
			throws Exception {
		ChecklistItemModelExt query = new ChecklistItemModelExt();
		query.setIdChecklist(idChecklist);

		GenericDal<ChecklistItemModel, ChecklistItemModelExt> dal = new GenericDal<ChecklistItemModel, ChecklistItemModelExt>(
				new ChecklistItemModelExt());
		List<ChecklistItemModel> list = dal.selectList(query);
		return list;
	}

	public static List<ChecklistItemModel> getChecklistItemList(boolean sorted,
			String sortField) throws Exception {
		loadChecklistItemList();
		// TODO : sort
		return _checklistItemList;
	}

	public static String getChecklistItemName(int idChecklistItem)
			throws Exception {
		String name = "";

		loadChecklistItemList();
		ChecklistItemModel obj = ModelUtil.<ChecklistItemModel> getFromList(
				_checklistItemList, idChecklistItem);
		if (obj != null && obj.getName() != null) {
			name = obj.getName();
		}

		return name;
	}

	// End of ChecklistItem

	// EVENT begin

	private static List<EventModel> _eventList = null;
	private static boolean _eventChanged = false;

	public static List<EventModel> getEventList(int idProject) throws Exception {
		EventModelExt query = new EventModelExt();
		query.setIdProject(idProject);

		GenericDal<EventModel, EventModelExt> dal = new GenericDal<EventModel, EventModelExt>(
				new EventModelExt());
		List<EventModel> list = dal.selectList(query);
		return list;
	}

	private static void loadEventList() throws Exception {
		if (_eventList == null || _eventChanged) {

			GenericDal<EventModel, EventModelExt> dalEvent = new GenericDal<EventModel, EventModelExt>(
					new EventModelExt());
			EventModelExt queryEvent = new EventModelExt();

			if (_eventList == null) {
				// first time
				_eventList = dalEvent.selectList(queryEvent);
			} else if (_eventChanged) {
				Date lastUpdated = ModelUtil.getLastUpdated(_eventList);

				// load records that have changed after the last updated
				queryEvent.setUpdatedFrom(lastUpdated);

				List<EventModel> list = dalEvent.selectList(queryEvent);

				// update new or changed records to cache
				ModelUtil.updateList(_eventList, list);
			}

			// set vanue changed as false
			_eventChanged = false;
		}
	}

	public static List<EventModel> getEventList() throws Exception {
		loadEventList();
		return _eventList;
	}

	public static List<EventModel> getEventList(boolean sorted,
			Comparator<EventModel> comparator) throws Exception {
		loadEventList();
		Collections.sort(_eventList, comparator);
		return _eventList;
	}

	public static String getEventName(int idEvent) throws Exception {
		String name = "";
		loadEventList();
		EventModel obj = ModelUtil
				.<EventModel> getFromList(_eventList, idEvent);
		if (obj != null && obj.getName() != null) {
			name = obj.getName();
		}
		return name;
	}

	// EVENT end

	// Purchase
	private static List<PurchaseModel> _purchaseList = null;
	private static boolean _purchaseChanged = false;

	private static void loadPurchaseList() throws Exception {

		if (_purchaseList == null || _purchaseChanged) {
			GenericDal<PurchaseModel, PurchaseModelExt> dalPurchase = new GenericDal<PurchaseModel, PurchaseModelExt>(
					new PurchaseModelExt());
			PurchaseModelExt queryPurchase = new PurchaseModelExt();
			queryPurchase.setOffset(Integer.MIN_VALUE);
			queryPurchase.setLimit(Integer.MIN_VALUE);

			_purchaseList = dalPurchase.selectList(queryPurchase);

			_purchaseChanged = false;
		}
	}

	public static List<PurchaseModel> getPurchaseList() throws Exception {

		loadPurchaseList();

		// TODO : sort

		return _purchaseList;
	}

	public static List<PurchaseModel> getPurchaseList(boolean sorted,
			String sortField) throws Exception {

		loadPurchaseList();

		// TODO : sort

		return _purchaseList;
	}

	public static String getPurchaseName(int idPurchase) throws Exception {
		String name = "";
		loadPurchaseList();
		PurchaseModel obj = ModelUtil.<PurchaseModel> getFromList(
				_purchaseList, idPurchase);
		if (obj != null && obj.getName() != null) {
			name = obj.getName();
		}
		return name;
	}

	public static PurchaseModel getPurchase(int idPurchase) throws Exception {
		loadPurchaseList();
		PurchaseModel obj = ModelUtil.<PurchaseModel> getFromList(
				_purchaseList, idPurchase);
		return obj;
	}

	// End of Purchase

	// PURCHASEITEM
	private static List<PurchaseItemModel> _purchaseItemList = null;
	private static boolean _purchaseItemChanged = false;

	private static void loadPurchaseItemList() throws Exception {

		if (_purchaseItemList == null || _purchaseItemChanged) {
			GenericDal<PurchaseItemModel, PurchaseItemModelExt> dalPurchaseItem = new GenericDal<PurchaseItemModel, PurchaseItemModelExt>(
					new PurchaseItemModelExt());
			PurchaseItemModelExt queryPurchaseItem = new PurchaseItemModelExt();
			queryPurchaseItem.setOffset(Integer.MIN_VALUE);
			queryPurchaseItem.setLimit(Integer.MIN_VALUE);

			_purchaseItemList = dalPurchaseItem.selectList(queryPurchaseItem);

			_purchaseItemChanged = false;
		}
	}

	public static List<PurchaseItemModel> getPurchaseItemList()
			throws Exception {

		loadPurchaseItemList();

		// TODO : sort

		return _purchaseItemList;
	}

	public static List<PurchaseItemModel> getPurchaseItemList(boolean sorted,
			String sortField) throws Exception {

		loadPurchaseItemList();

		// TODO : sort

		return _purchaseItemList;
	}

	public static List<PurchaseItemModel> getPurchaseItemList(int idPurchase)
			throws Exception {
		PurchaseItemModelExt query = new PurchaseItemModelExt();
		query.setIdPurchase(idPurchase);

		GenericDal<PurchaseItemModel, PurchaseItemModelExt> dal = new GenericDal<PurchaseItemModel, PurchaseItemModelExt>(
				new PurchaseItemModelExt());
		List<PurchaseItemModel> list = dal.selectList(query);
		return list;
	}

	public static String getPurchaseItemName(int idPurchaseItem)
			throws Exception {
		String name = "";
		loadPurchaseItemList();
		PurchaseItemModel obj = ModelUtil.<PurchaseItemModel> getFromList(
				_purchaseItemList, idPurchaseItem);
		if (obj != null && obj.getName() != null) {
			name = obj.getName();
		}
		return name;
	}

	// END OF PURCHASE ITEM

	// DOCUMENT

	private static List<DocumentModel> _documentList = null;
	private static boolean _documentChanged = false;

	private static void loadDocumentList() throws Exception {
		if (_documentList == null || _documentChanged) {
			GenericDal<DocumentModel, DocumentModelExt> dalDocument = new GenericDal<DocumentModel, DocumentModelExt>(
					new DocumentModelExt());
			DocumentModelExt queryDocument = new DocumentModelExt();
			_documentList = dalDocument.selectList(queryDocument);
			_documentChanged = false;
		}
	}

	public static List<DocumentModel> getDocumentList() throws Exception {
		loadDocumentList();
		// TODO : sort
		return _documentList;
	}

	public static List<DocumentModel> getDocumentList(boolean sorted,
			String sortField) throws Exception {
		loadDocumentList();
		// TODO : sort
		return _documentList;
	}

	public static String getDocumentName(int idDocument) throws Exception {
		String name = "";
		loadDocumentList();
		DocumentModel obj = ModelUtil.<DocumentModel> getFromList(
				_documentList, idDocument);
		if (obj != null && obj.getName() != null) {
			name = obj.getName();
		}
		return name;
	}

	// END OF DOCUMENT
	// DOCUMENT TYPE

	private static List<DocumentTypeModel> _documentTypeList = null;
	private static boolean _documentTypeChanged = false;

	private static void loadDocumentTypeList() throws Exception {
		if (_documentTypeList == null || _documentTypeChanged) {
			GenericDal<DocumentTypeModel, DocumentTypeModelExt> dalDocumentType = new GenericDal<DocumentTypeModel, DocumentTypeModelExt>(
					new DocumentTypeModelExt());
			DocumentTypeModelExt queryDocumentType = new DocumentTypeModelExt();
			_documentTypeList = dalDocumentType.selectList(queryDocumentType);
			_documentTypeChanged = false;
		}
	}

	public static List<DocumentTypeModel> getDocumentTypeList()
			throws Exception {
		loadDocumentTypeList();
		// TODO : sort
		return _documentTypeList;
	}

	public static List<DocumentTypeModel> getDocumentTypeList(boolean sorted,
			String sortField) throws Exception {
		loadDocumentTypeList();
		// TODO : sort
		return _documentTypeList;
	}

	public static String getDocumentTypeName(int idDocumentType)
			throws Exception {
		String name = "";
		loadDocumentTypeList();
		DocumentTypeModel obj = ModelUtil.<DocumentTypeModel> getFromList(
				_documentTypeList, idDocumentType);
		if (obj != null && obj.getName() != null) {
			name = obj.getName();
		}
		return name;
	}

	// END OF DOCUMENT TYPE
}